﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace kripto2._1
{
    public partial class BlockCipherForm : Form
    {
        //ATRIBUT
        string filename; // file to encript or decript
        byte[] message;
        int chooseMode = 0;
        byte blockSize = 64;
        byte[] iv;

        public BlockCipherForm()
        {
            InitializeComponent();
        }

        private void browse_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile1 = new OpenFileDialog();

            openFile1.Title = "Cari nama file pesan";
            openFile1.ShowDialog();

            if (openFile1.FileName != "")
            {
                filename = openFile1.SafeFileName;
                FileStream fs = new FileStream(openFile1.FileName, FileMode.Open, FileAccess.Read);
                message = null;
                BinaryReader abc = new BinaryReader(fs);
                long numBYtes = new FileInfo(openFile1.FileName).Length;

                if (numBYtes < 64)
                {
                    MessageBox.Show("ukuran data kurang besar. minimal 64bit");
                }
                else
                {
                    message = abc.ReadBytes((int)numBYtes);
                    browse_textarea.Text = openFile1.FileName;
                }
            }
        }

        private void encript_MouseClick(object sender, MouseEventArgs e)
        {
            chooseMode = combochooseMode.SelectedIndex;
            message = new byte[8];
            message[0] = 1;
            message[1] = 2;
            message[2] = 3;
            message[3] = 4;
            message[4] = 5;
            message[5] = 6;
            message[6] = 7;
            message[7] = 8;

            iv = new byte[key.Text.Length + (key.Text.Length % 2)];
            Random r = new Random();
            for (int i = 0; i < iv.Length; i++)
            {
                iv[i] = (byte)r.Next(255);
            }

            if (message != null)
            {
                if (chooseMode == 0)  //ECB
                {
                    message = algorithm.ECB.encrypt(message,
                        operation.generateTrueKey.getTrueKey(key.Text));
                }
                else if (chooseMode == 1) //CBC
                {
                    message = algorithm.CBC.encrypt(message,
                        operation.generateTrueKey.getTrueKey(key.Text), iv);
                }
                else if (chooseMode == 2) //CFB
                {
                    message = algorithm.CFB.encrypt(message,
                        operation.generateTrueKey.getTrueKey(key.Text), iv);
                }
                else if (chooseMode == 3) //OFB
                {
                    message = algorithm.OFB.encrypt(message,
                        operation.generateTrueKey.getTrueKey(key.Text), iv);
                }
                Console.WriteLine("hasil enkripsi");

                for (int i = 0; i < message.Length; i++)
                {
                    Console.Write(message[i] + " ");
                }
                Console.WriteLine("\n");
            }
        }

        private byte[] getHeaderCipher()
        {
            byte[] header;

            header = null;

            return header;
        }

        private void resetAllVar()
        {
            message = null;
            filename = "";
        }

        private void decript_MouseClick(object sender, EventArgs e)
        {
            chooseMode = combochooseMode.SelectedIndex;
            message = new byte[8];
            message[0] = 160;
            message[1] = 130;
            message[2] = 15;
            message[3] = 138;
            message[4] = 104;
            message[5] = 89;
            message[6] = 214;
            message[7] = 34;

            iv = new byte[key.Text.Length + (key.Text.Length % 2)];
            Random r = new Random();
            for (int i = 0; i < iv.Length; i++)
            {
                iv[i] = (byte)r.Next(255);
            }
            if (message != null)
            {
                if (chooseMode == 0)  //ECB
                {
                    message = algorithm.ECB.decrypt(message,
                        operation.generateTrueKey.getTrueKey(key.Text));
                }
                else if (chooseMode == 1) //CBC
                {
                    message = algorithm.CBC.decrypt(message,
                        operation.generateTrueKey.getTrueKey(key.Text), iv);
                }
                else if (chooseMode == 2) //CFB
                {
                    message = algorithm.CFB.decrypt(message,
                        operation.generateTrueKey.getTrueKey(key.Text), iv);
                }
                else if (chooseMode == 3) //OFB
                {
                    message = algorithm.OFB.decrypt(message,
                        operation.generateTrueKey.getTrueKey(key.Text), iv);
                }
                Console.WriteLine("hasil dekripsi");
                for (int i = 0; i < message.Length; i++)
                {
                    Console.Write(message[i] + " ");
                }
                Console.WriteLine("\n");
            }
        }
    }
}
